package product;

import java.awt.EventQueue;

import javax.swing.JInternalFrame;

import java.awt.BorderLayout;

import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.JButton;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.Vector;

public class MemberUpdateLogin extends JInternalFrame {
	public JLabel label;
	public JTextField adId;
	public JLabel label_1;
	public JTextField adPass;
	public JButton btnLogin;
	MemberUpdateLogin ml;
	Connection conn;
	ProductMain3 pm;
	PreparedStatement pstmt;
	ResultSet rs;
	ResultSetMetaData rsmd;
	MemberInput mi;
	MemberSelect ms;
	DefaultTableModel model = new DefaultTableModel();
	Vector<String> v;
	TableRowSorter<TableModel> tSoter;
	boolean tFlag = false;
	private JTextField txtaza;

	public MemberUpdateLogin(Connection conn, ProductMain3 pm) {
		super("로그인", true, true, true, false);
		this.conn=conn;
		this.pm=pm;
		setBounds(100, 100, 255, 157);
		getContentPane().setLayout(null);
		getContentPane().add(getLabel());
		getContentPane().add(getAdId());
		getContentPane().add(getLabel_1());
		getContentPane().add(getAdPass());
		getContentPane().add(getBtnLogin());
		getContentPane().add(getTxtaza());

	}
	private JLabel getLabel() {
		if (label == null) {
			label = new JLabel("관리자");
			label.setBounds(12, 14, 57, 15);
		}
		return label;
	}
	public JTextField getAdId() {
		if (adId == null) {
			adId = new JTextField();
		//	adId.setText("AZ");
			adId.setHorizontalAlignment(SwingConstants.CENTER);
			adId.setColumns(10);
			adId.setBounds(74, 10, 155, 21);
		}
		return adId;
	}
	private JLabel getLabel_1() {
		if (label_1 == null) {
			label_1 = new JLabel("비밀번호");
			label_1.setBounds(12, 44, 57, 15);
		}
		return label_1;
	}
	public JTextField getAdPass() {
		if (adPass == null) {
			adPass = new JTextField();
			adPass.setHorizontalAlignment(SwingConstants.CENTER);
			adPass.setColumns(10);
			adPass.setBounds(74, 41, 155, 21);
		}
		return adPass;
	}
	public void adLogin(){
		//관리자 아이디로 접속이 되었을때,
		try{	
			String mid=adId.getText();
			if(adId.getText().equals("AZ")){
				String sql = "select * from memberdata where memberid=?";
				pstmt = conn.prepareStatement(sql);
				pstmt.setString(1, "AZ");
				rs = pstmt.executeQuery();
				
				while(rs.next()){
					String key = rs.getString("pwd");
					if(key.equals(adPass.getText())){
					
						pm.mntmMemberinput.setVisible(true);
						pm.mntmMemberupdate.setVisible(true);
						pm.mntmMembermonth.setVisible(true);
						pm.mntmMemberholy.setVisible(true);
						pm.mntmMemberinout.setVisible(true);
						
						if(pm.mLogin!=null){
							pm.mLogin.setVisible(false);
						}
						
						adPass.setText("****");

						adId.setEnabled(false);
						adPass.setEnabled(false);
						btnLogin.setEnabled(false);
						tFlag = true;
						JOptionPane.showMessageDialog(null, "관리자 로그인 되었습니다.");
						pm.logincode=adId.getText();
						pm.setTitle(pm.logincode);
						
					
			
					}
				}
				if(tFlag == false){
					JOptionPane.showMessageDialog(null, "패스워드가 다릅니다.");
				}

			}else if(adId.getText().equals(mid)){
				String sql = "select * from memberdata where memberid=?";
				pstmt = conn.prepareStatement(sql);
				pstmt.setString(1, mid);
				rs = pstmt.executeQuery();
				
				while(rs.next()){
					String key = rs.getString("pwd");
					if(key.equals(adPass.getText())){
						
						//pm.mntmMemberinput.setVisible(true);
						//pm.mntmMemberupdate.setVisible(true);
						//pm.mntmMembermonth.setVisible(true);
						pm.mntmMemberholy.setVisible(true);
						pm.mntmMemberinout.setVisible(true);
						if(pm.mLogin!=null){
							pm.mLogin.setVisible(false);
						}
						
						adPass.setText("****");

						adId.setEnabled(false);
						adPass.setEnabled(false);
						btnLogin.setEnabled(false);
						tFlag = true;
						pm.logincode=adId.getText();
						pm.setTitle(pm.logincode);
						JOptionPane.showMessageDialog(null, "사원 로그인 되었습니다.");
						
			
					}
				}
				if(tFlag == false){
					JOptionPane.showMessageDialog(null, "패스워드가 다릅니다.");
				}
			}
			else{
				JOptionPane.showMessageDialog(null, "관리자 아이디가 아닙니다.");
			}

		}catch(Exception e){
			e.printStackTrace();
		}

	}

	public JButton getBtnLogin() {
		if (btnLogin == null) {
			btnLogin = new JButton("Login");
			btnLogin.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {//로그인
					adLogin();
				
				}
			});
			btnLogin.setBounds(74, 72, 155, 21);
		}
		return btnLogin;
	}
	private JTextField getTxtaza() {
		if (txtaza == null) {
			txtaza = new JTextField();
			txtaza.setEnabled(false);
			txtaza.setEditable(false);
			txtaza.setText("관리자:AZ/1234, 사원:A숫자/1234");
			txtaza.setBounds(10, 99, 219, 21);
			txtaza.setColumns(10);
		}
		return txtaza;
	}
}
